<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Category
 *
 * @author Eugene
 */
class Application_Model_Mapper_Category extends Application_Model_Mapper_Abstract
{

	/**
	 * @var string
	 */
	protected $_dbTableClass = 'Application_Model_DbTable_Category';

	/**
	 *
	 * @param Application_Model_Abstract $model
	 * @return void
	 */
	public function save(Application_Model_Abstract $model)
	{
		$data = array(
			'name' => $model->getName(),
			'order' => $model->getOrder(),
			'image' => $model->getImage(),
			'enabled' => $model->getEnabled(),
			'parentId' => $model->getParentId(),
			'description' => $model->getDescription()
		);

		if ($model->getId() > 0) {
			$this->getDbTable()->update($data, array('id = ?' => $model->getId()));
		} else {
			$this->getDbTable()->insert($data);
		}
	}

	/**
	 *
	 * @return array
	 */
	public function fetchAll()
	{
		$results = $this->getDbTable()->fetchAll();
		$entries = array();

		foreach ($results as $row) {
			$entry = new Application_Model_Category;
			$entry->setOptions($row->toArray());

			$entries[] = $entry;
		}

		return $entries;
	}

}